<?php

class Color extends CI_Controller {
    
    public $_updir = 'public/template/backend/images/products/';
    private $_updat;
    private $_auth;
    public function __construct() {
        parent::__construct();
        $this->_auth = $this->myclass->checkuser();
        if($this->_auth == FALSE) redirect ("backend/home");
    }
    
    public function view($id){
        $data['data']['parcat']= $this->mcolor->getByProId($id);
        $data['data']['pro_id']= $id;
        $data['title'] = "Bảng màu";
        $data['temp'] = "backend/color/home";
        $this->load->view("backend/layout/index",$data);
    }
    
    public function add($id) {
        if ($this->input->post("add")) {
            $post = $this->input->post("data");
            $this->form_validation->set_error_delimiters('<li>', '</li>');
            $this->form_validation->set_rules('data[color]', 'Màu', 'trim|required');
            $this->form_validation->set_rules('data[price_bonus]', 'Giá thêm', 'trim|required|integer');        
            $config['upload_path'] = $this->_updir;
            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_size'] = '2000';
            $config['overwrite'] = TRUE;
            $this->load->library('upload', $config);
            if ($this->form_validation->run() == TRUE) {
                if ($this->upload->do_upload('img')) {
                    $this->_updat = $this->upload->data();
                    $post['img'] = $this->_updir . $this->_updat['file_name']; 
                    $post['pro_id'] = $id;
                    $this->mcolor->insert($post);
                    redirect("backend/color/view" . "/" . $id);
                } else {
                    $data['data'] = array('error' => $this->upload->display_errors());
                }
            }
        }
        $data['data']['pro_id'] = $id;        
        $data['title'] = "Thêm màu";
        $data['temp'] = "backend/color/form";
        $this->load->view("backend/layout/index", $data);
    }
    
    public function update($id) {
        $data['data']['pro'] = $this->mcolor->fetchOne($id);
        if ($this->input->post("add")) {
            $post = $this->input->post("data");
            $this->form_validation->set_error_delimiters('<li>', '</li>');
            $this->form_validation->set_rules('data[color]', 'Màu', 'trim|required');
            $this->form_validation->set_rules('data[price_bonus]', 'Giá thêm', 'trim|required|integer');        
                      
            if ($this->form_validation->run() == TRUE) {
                $config['upload_path'] = $this->_updir;
                $config['allowed_types'] = 'gif|jpg|png';
                $config['max_size'] = '2000';
                $config['overwrite'] = TRUE;
                $this->load->library('upload', $config);
                if ($this->upload->do_upload('img')) {
                    $this->_updat = $this->upload->data();
                    $post['img'] = $this->_updir . $this->_updat['file_name'];
                } else {
                    $post['img'] = $data['data']['pro']->img;
                }
                $this->mcolor->update($id, $post);
                redirect("backend/color/view" . "/" . $data['data']['pro']->pro_id);
            }
        }
        $data['data']['pro_id'] = $data['data']['pro']->pro_id;     
        $data['title'] = "Sửa màu";
        $data['temp'] = "backend/color/form";
        $this->load->view("backend/layout/index", $data);
    }
    
    public function delete($id,$pro_id){
        $this->mcolor->delete($id);
        $this->mimages->deleteByColor($id);
        redirect("backend/color/view" . "/" . $pro_id);
    }
    
    public function deleteall($id){
        $this->mcolor->deleteAll($id);
        $this->mimages->deleteAllByColor($id);
        redirect("backend/color/view" . "/" . $id);
    }

}
